package com.apexedu.portal.action;

import com.apexedu.framework.service.BaseService;
import com.apexedu.framework.util.SeqFactory;
import com.apexedu.sys.action.AbstractBaseActionSupport;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 门户logo维护
 * Created by janabo on 2014-10-11.
 */

@SuppressWarnings("serial")
@Component
@Scope("prototype")
public class PortalLogoAction  extends AbstractBaseActionSupport {

    private Map<String, Object> datamap = new HashMap<String, Object>();
    private String logoImg;
    private File logo;//上传logo图片
    private String logoFileName;//上传的logo图片名
    private String logoContentType;

    /**
     * 此方法植入service类
     */
    @Resource(name="portalLogoService")
    public void setBaseService(BaseService baseService){
        super.setBaseService(baseService);
    }

    public String getLogoImg() {
        return logoImg;
    }

    public void setLogoImg(String logoImg) {
        this.logoImg = logoImg;
    }

    public File getLogo() {
        return logo;
    }

    public void setLogo(File logo) {
        this.logo = logo;
    }

    public String getLogoFileName() {
        return logoFileName;
    }

    public void setLogoFileName(String logoFileName) {
        this.logoFileName = logoFileName;
    }

    /**
     * 修改logo
     * @return
     */
    public String updateLogo() {
        boolean flag = true;
        String tpm = getRequest().getParameter("tpm");
        try {
            updateTpm(tpm);
        }catch (Exception e){
            flag = false;
            e.printStackTrace();
        }
        datamap.put("isSuccess",flag);
        return "SUCCESS";
    }

    public void updateTpm(String tpm){
        String savePath = ServletActionContext.getRequest().getRealPath("/upload/");
        File file = new File(savePath);
        for (File f : file.listFiles()) {
            String filename = f.getName();
            if(filename.toLowerCase().contains("logo")){
                if(filename.indexOf(".")>=0)
                {
                    filename   =  "/z"+ SeqFactory.getNewSequenceAlone() + filename.substring(filename.lastIndexOf("."));
                }else {
                    filename =  "/z"+SeqFactory.getNewSequenceAlone() + ".jpg";
                }
                f.renameTo(new File(savePath+filename));
            }
        }
        for(File f : file.listFiles()){
            String filename = f.getName();
            if(filename.toLowerCase().contains(tpm)){
                if(filename.indexOf(".")>=0)
                {
                    filename   =  "/logo" + filename.substring(filename.lastIndexOf("."));
                }else {
                    filename =  "/logo" + ".jpg";
                }
                f.renameTo(new File(savePath+filename));
            }
        }
    }
}
